﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace bzxxxt.admin.service
{
    public class Utils
    {
        private string connStr = ConfigurationManager.ConnectionStrings["newconn"].ConnectionString;
        private string path = ConfigurationManager.AppSettings["path"];
        private string dbname = ConfigurationManager.AppSettings["dbname"];

        public static string getSectionStr(string section)
        {
            if (section.Length < 4)
            {
                section = "00" + section;
                section = section.Substring(section.Length - 3);
            }            
            return section;
        }
        public static string getSectionStr4Bit(string section)
        {
            if (section.Length < 4)
            {
                section = "00" + section;
                section = "1"+section.Substring(section.Length - 3);

            }
            return section;
        }
        /**
    * 备份数据库
    * @param path
    * @throws SQLException
    */
        public void backup()
        {
            string path2 = path.Replace("/", "\\");
            string filename = path2 + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak";
            string sql = "BACKUP DATABASE " + dbname + " TO DISK = '" + filename + "'";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            conn.ChangeDatabase("master");
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
            conn.ChangeDatabase(dbname);
            cmd.CommandText = "insert into tbBack(backupfile) values('" + filename + "')";
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        /**
         * 恢复数据库
         * @param path
         * @throws SQLException
         */
        public void restore(String filename)
        {
            string setOffline = "Alter database " + dbname + " Set Offline With rollback immediate; ";
            string sql = "RESTORE DATABASE " + dbname + " FROM DISK = '" + filename + "' WITH REPLACE;";            
            string setOnline = " Alter database " + dbname + " Set Online With Rollback immediate;";           
            SqlConnection conn = new SqlConnection(connStr);            
            conn.Open();
            conn.ChangeDatabase("master");
            SqlCommand cmd = new SqlCommand(setOffline + sql + setOnline, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public string getBackupFileList() {
            string sql = "SELECT id,backupfile,backupdate FROM tbBack order by backupdate DESC";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd.ExecuteReader();
            List<TbBack> list = new List<TbBack>();
            while (dr.Read()) {
                TbBack b = new TbBack { 
                    id = dr["id"].ToString(),
                    backupfile = dr["backupfile"].ToString(),
                    backupdate = DateTime.Parse(dr["backupdate"].ToString()).ToString()
                };
                list.Add(b);
            }
            
            conn.Close();
            return JsonUtil.ToJson(list);
        }
    }

    public class TbBack {
        public string id { get; set; }
        public string backupfile{get;set;}
        public string backupdate { get; set; }
    }
}